using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace JSG.BubbleShoot
{
    public class Bomb : MonoBehaviour
    {
        private bool m_ShakeScale;
        private Vector3 Startpos;
        public GameObject m_DestroyParticle;
        // Start is called before the first frame update
        void Start()
        {
            Startpos.x = transform.position.x;
        }

        // Update is called once per frame
        void Update()
        {
            if (m_ShakeScale)
            {
                transform.localScale += new Vector3(.003f, .003f, 0f);
                transform.position = new Vector3(Startpos.x + (Mathf.Sin(Time.time * 50) * .5f), transform.position.y, transform.position.z);
            }
        }

        private void OnCollisionEnter2D(Collision2D collision)
        {
            if (collision.gameObject.tag == "Player")
            {
                m_ShakeScale = true;
                GameControl.m_Current.m_Explosion = true;
                Invoke("Explode", .6f);
            }
        }

        public void Explode()
        {
            CameraControl.Current.StartShake(.5f, 10);

            foreach (Candy candy in GameControl.m_Current.m_Candies)
            {
                if (!candy.gameObject.activeSelf)
                    continue;

                float dis = Vector3.Distance(candy.transform.position, transform.position);
                if (dis <= 25)
                {
                    candy.DestroyAlone();
                }
            }

            GameObject obj = Instantiate(m_DestroyParticle);
            obj.transform.position = transform.position + new Vector3(0, 0, -2);
            Destroy(obj, 3);

            Destroy(gameObject);
        }
    }
}